// <copyright file="CmsDataContext.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Data
{
    using System.Data.Entity;
    using Cms.Data.Entities;
    using System.Data.Entity.Migrations;

    public class CmsDataContext : DbContext
    {
        public CmsDataContext() : base("DataContext") { }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            #region Authentication 
            
            modelBuilder.Entity<UserProfile>().ToTable("UserProfile");
            modelBuilder.Entity<OAuthMembership>().ToTable("webpages_OAuthMembership");
            modelBuilder.Entity<Membership>().ToTable("webpages_Membership");
            modelBuilder.Entity<Role>().ToTable("webpages_Roles");

            modelBuilder.Entity<Membership>()
            .HasMany<Role>(r => r.Roles)
            .WithMany(u => u.Members)
            .Map(m =>
            {
                m.ToTable("webpages_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });
            #endregion

            #region CMS

            modelBuilder.Entity<CmsMenuItem>().ToTable("MenuItem", "cms");
            modelBuilder.Entity<CmsMenuType>().ToTable("MenuType", "cms");
            modelBuilder.Entity<CmsSetting>().ToTable("Setting", "cms");
            modelBuilder.Entity<CmsLog>().ToTable("Log", "dbo");
            modelBuilder.Entity<CmsPage>().ToTable("Page", "cms");
            modelBuilder.Entity<CmsPageCategory>().ToTable("PageCategory", "cms");
            modelBuilder.Entity<CmsTag>().ToTable("PageTag", "cms");
            modelBuilder.Entity<CmsStatus>().ToTable("PageStatus", "cms");
            modelBuilder.Entity<CmsRating>().ToTable("PageRating", "cms");
            modelBuilder.Entity<CmsPageComment>().ToTable("PageComment", "cms");

            modelBuilder.Entity<CmsTag>()
                .HasMany(x => x.Pages)
                .WithMany(x => x.Tags)
            .Map(x =>
            {
                x.ToTable("PageTags", "cms");
                x.MapRightKey("Id");
                x.MapLeftKey("TagId");
            });

            #endregion
           
        }

        public DbSet<CmsMenuItem>       CmsMenuItems { get; set; }
        public DbSet<CmsMenuType>       CmsMenuTypes { get; set; }
        public DbSet<CmsPage>           CmsPages { get; set; }
        public DbSet<CmsSetting>        CmsSettings { get; set; }
        public DbSet<CmsTag>            CmsTags { get; set; }
        public DbSet<UserProfile>       UserProfiles { get; set; }
        public DbSet<CmsPageCategory>   CmsPageCategories { get; set; }
        public DbSet<CmsStatus>         Statuses { get; set; }
        public DbSet<CmsLog>            Logs { get; set; }
        public DbSet<CmsRating>         PageRatings { get; set; }
        public DbSet<CmsPageComment>    PageComments { get; set; }
    }
}